/*
 * MonsterType.java
 *
 * Created on January 6, 2008, 4:04 AM
 *
 * To change this template, choose Tools | Template Manager
 * and open the template in the editor.
 */

package wisp.dataObjects;
import java.sql.*;
import java.io.*;
import java.util.*;
import java.util.Vector;
import java.util.StringTokenizer;
import java.lang.Exception.*;
/**
 *
 * @author Nezie Santos
 */
public class MonsterType {
     private dbConnection dbcon = new dbConnection();
     private String mName, fname, description, mpic;
     private int mHP,eGained, mlevel;
    /** Creates a new instance of MonsterType */
    public MonsterType() {
    }
    public MonsterType(String mname, String field, String des, int maxhp, int egained, int mlevel) {
        setMName(mname);
        setFname(field);
        setDescription(des);
        setMHP(maxhp);
        setEGained(egained);
        setLevel(mlevel);
    }
    public boolean checkExist(String name) 
    {
        //Statement stmt;
        String query;
        ResultSet rs;
        PreparedStatement stmt;
        boolean exist = false;
                
        getDbcon().connect();
        query = "SELECT * FROM MonsterType WHERE monsterName = ?";
        
        try
        {
             	
            stmt = getDbcon().con.prepareStatement(query);
            stmt.setString(1, name);
            rs = stmt.executeQuery();	
        
            while (rs.next()) 
            {	
              exist = true;
             }
            rs.close(); 					
            stmt.close();					
        } catch (Exception ex)
        {
            System.out.println(ex.getMessage());	       	
        }
        getDbcon().disconnect();
        return exist;
    }
    public void addMType (MonsterType m){
        String query; 					
        ResultSet rs;
        PreparedStatement stmt;
        
        if (!checkExist(m.getMName()))
        {
            getDbcon().connect();
            query = "INSERT INTO MonsterType (monsterName, maxHP, eGained, mlevel, field, description) VALUES (?,?,?,?,?,?)";
            try 
            {
                stmt = getDbcon().con.prepareStatement(query);
                stmt.setInt(1, m.getMHP());
                stmt.setInt(2, m.getMHP());
                stmt.setInt(3, m.getEGained());
                stmt.setInt(4, m.getLevel());
                stmt.setString(5, m.getFname());
                stmt.setString(6, m.getDescription());
                stmt.executeUpdate();	
                stmt.close();
            } catch (Exception ex) 
            {
                System.out.println(ex.getMessage());
            }
            getDbcon().disconnect();
        }
    }
    
     public void deleteMonsterType (String name) {
        		
    	String query;		
    	ResultSet rs;   
        PreparedStatement stmt;
        
       	getDbcon().connect();						
        query = "DELETE MonsterType WHERE monsterName = ?";
        
        try 
        {
            stmt = getDbcon().con.prepareStatement(query);	
            stmt.setString(1, name);
            stmt.executeUpdate();				
            stmt.close();						
        } catch (Exception ex) 
        {
            System.out.println(ex.getMessage());	       	
        }
      }
   
    
     public void viewTInfo (String name){
        String query;
        ResultSet rs;
        PreparedStatement stmt;
                
        getDbcon().connect();
        query = "SELECT monsterName, maxHP, eGained, mlevel, field, description, mpic FROM MonsterType WHERE monsterName = ?";
        
        try
        {
            stmt = getDbcon().con.prepareStatement(query);
            stmt.setString(1, name);
            rs = stmt.executeQuery();	
        
            while (rs.next()) 
            {
                this.setMName(rs.getString("monsterName"));
                this.setMHP(rs.getInt("maxHP"));
                this.setEGained(rs.getInt("eGained"));
                this.setLevel(rs.getInt("mlevel"));
                this.setFname(rs.getString("field"));
                this.setDescription(rs.getString("description"));
                this.setMpic(rs.getString("mpic"));
            }
            rs.close(); 					
            stmt.close();
        } catch (Exception ex)
        {
            System.out.println(ex.getMessage());	       	
        }
        finally {
            getDbcon().disconnect();
 
        }
    }
     
     public Vector viewAllMonsters ()
     {
        String query;
        ResultSet rs;
        PreparedStatement stmt;
        Vector<MonsterType> mList = new Vector<MonsterType>();
                
        getDbcon().connect();
        query = "SELECT * FROM MonsterType";
        
        try
        {
            stmt = getDbcon().con.prepareStatement(query);
            rs = stmt.executeQuery();
            MonsterType m = null;
        
            while (rs.next()) 
            {
                m = new MonsterType();
                m.setMName(rs.getString("monsterName"));
                m.setMHP(rs.getInt("maxHP"));
                m.setEGained(rs.getInt("eGained"));
                m.setLevel(rs.getInt("mlevel"));
                m.setFname(rs.getString("field"));
                m.setDescription(rs.getString("description"));
                 m.setMpic(rs.getString("mpic"));
                mList.add(m);
            }
            rs.close(); 					
            stmt.close();
            return mList;
        } catch (Exception ex)
        {
            System.out.println(ex.getMessage());	       	
        }
        finally {
            getDbcon().disconnect();
 
        }
        return null;
    }
     
     public Vector selectMonster (String p1, String p2){
        //Statement stmt;
        String query;
        ResultSet rs;
        PreparedStatement stmt;
        Vector<MonsterType> mList;
            
        mList = new Vector<MonsterType>();
        Iterator it = mList.iterator();
                
        getDbcon().connect();
        query = "SELECT monsterName, maxHP, eGained, mlevel, field, description, mpic FROM MonsterType WHERE field = ? OR field = ?";
        
        try
        {
            stmt = getDbcon().con.prepareStatement(query);
            stmt.setString(1, p1);
            stmt.setString(2, p2);
            rs = stmt.executeQuery();	
        
            while (rs.next()) 
            {	
                MonsterType m = new MonsterType();
                m.setMName(rs.getString("monsterName"));
                m.setMHP(rs.getInt("maxHP"));
                m.setEGained(rs.getInt("eGained"));
                m.setLevel(rs.getInt("mlevel"));
                m.setFname(rs.getString("field"));
                m.setDescription(rs.getString("description"));
                 m.setMpic(rs.getString("mpic"));
                mList.add(m);
                System.out.println("-----------------------------------------------> MERON MERON MERON");
              
             }
            //crset.populate(rs);
            rs.close(); 					
            stmt.close();
            return mList;
 				
        } catch (Exception ex)
        {
            System.out.println(ex.getMessage());	       	
        }
       finally {
            getDbcon().disconnect();
        }
        return null;
     }
     public void editMTInfo (String mName, String at, String sVal, int iVal) {
         						
        String query; 					
        ResultSet rs;
        PreparedStatement stmt;

        getDbcon().connect();
        if (at.equals("maxHP"))
            query = "UPDATE MonsterType SET maxHP = ? WHERE monsterName = ?";
        else if (at.equals("eGained"))
            query = "UPDATE MonsterType SET eGained = ? WHERE monsterName = ?";
        else if (at.equals("mlevel"))
            query = "UPDATE MonsterType SET mlevel = ? WHERE monsterName = ?";
        else if (at.equals("field"))
            query = "UPDATE MonsterType SET field = ? WHERE monsterName = ?";
        else
            query = "UPDATE MonsterType SET description = ? WHERE monsterName = ?";
        try 
        {
             
            stmt = getDbcon().con.prepareStatement(query);
            
            if (at.equals("maxHP") || at.equals("eGained") || at.equals("mlevel"))
                stmt.setInt(1, iVal);
            else
                stmt.setString(1, sVal);
            stmt.setString(2, mName);
            stmt.executeUpdate();	
            stmt.close();					
        } catch (Exception ex) 
        {
            System.out.println(ex.getMessage());	       	
        }
        getDbcon().disconnect();
     }

    public dbConnection getDbcon() {
        return dbcon;
    }

    public void setDbcon(dbConnection dbcon) {
        this.dbcon = dbcon;
    }

    public String getMName() {
        return mName;
    }

    public void setMName(String mName) {
        this.mName = mName;
    }

    public String getFname() {
        return fname;
    }

    public void setFname(String fname) {
        this.fname = fname;
    }

    public String getDescription() {
        return description;
    }

    public void setDescription(String description) {
        this.description = description;
    }

    public int getMHP() {
        return mHP;
    }

    public void setMHP(int mHP) {
        this.mHP = mHP;
    }

    public int getEGained() {
        return eGained;
    }

    public void setEGained(int eGained) {
        this.eGained = eGained;
    }

    public int getLevel() {
        return mlevel;
    }

    public void setLevel(int mlevel) {
        this.mlevel = mlevel;
    }

    public String getMpic() {
        return mpic;
    }

    public void setMpic(String mpic) {
        this.mpic = mpic;
    }
}
